/**
 * 
 */
package org.istt.export;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.istt.domain.FileCapCCHN;
import org.istt.domain.FileCapLaiCCHN;

/**
 * @author Ndtho
 *
 */
public class BmQT_CCHN_CL extends BaseReport{
	
	public InputStream toMSWord(InputStream docFile, FileCapLaiCCHN f) throws IOException {
	    
	    XWPFDocument docx = new XWPFDocument(docFile);
	    Iterator<XWPFTable> range=docx.getTablesIterator();

	    //Header
        XWPFTable tableHeader=range.next();
        
        //Contact Info
        XWPFTable tableContact=range.next();
        
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		Date today = new Date();
		String date =  dateFormat.format(today);
        
        /*==================== HEADER ====================*/
        XWPFTableRow header_row = tableHeader.getRow(1);
        setTextFormat(header_row.getCell(0).getParagraphs().get(0).createRun(), getText("report.BmQT1201.header.date.first")+ " Ng\u00E0y "+date.substring(0, 2)+" th\u00E1ng "+date.substring(3, 5)+" n\u0103m "+date.substring(6, 10), FONT_SIZE_12, false, true, FONT_TIMES_NEW_ROMAN);

        header_row = tableHeader.getRow(3);
        setTextFormat(header_row.getCell(0).getParagraphs().get(0).createRun(), f.getSubject(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);
        
        /*==================== CONTACT INFO ====================*/
        XWPFTableRow row = tableContact.getRow(0);
		setTextFormat(row.getCell(0).getParagraphs().get(0).createRun(), f.getFullname(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);
		setTextFormat(row.getCell(1).getParagraphs().get(0).createRun(), f.getSex(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);
		
        row = tableContact.getRow(1);
        setTextFormat(row.getCell(0).getParagraphs().get(0).createRun(), f.getDate_of_birth(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);
        
        row = tableContact.getRow(2);
        setTextFormat(row.getCell(0).getParagraphs().get(0).createRun(), f.getPlace_of_birth(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        row = tableContact.getRow(3);
        setTextFormat(row.getCell(0).getParagraphs().get(0).createRun(), f.getNationality(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        row = tableContact.getRow(4);
        setTextFormat(row.getCell(0).getParagraphs().get(0).createRun(), f.getIdcard(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);
        setTextFormat(row.getCell(1).getParagraphs().get(0).createRun(), f.getIdcard_date(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);
        
        row = tableContact.getRow(5);
        setTextFormat(row.getCell(0).getParagraphs().get(0).createRun(), f.getIdcard_location(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        row = tableContact.getRow(6);
        setTextFormat(row.getCell(0).getParagraphs().get(0).createRun(), f.getAddress(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        row = tableContact.getRow(7);
        setTextFormat(row.getCell(0).getParagraphs().get(0).createRun(), f.getLevel(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);
        
        row = tableContact.getRow(8);
        setTextFormat(row.getCell(0).getParagraphs().get(0).createRun(), f.getDiploma(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        row = tableContact.getRow(9);
        setTextFormat(row.getCell(0).getParagraphs().get(0).createRun(), f.getCertificate(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        row = tableContact.getRow(11);
        setTextFormat(row.getCell(0).getParagraphs().get(0).createRun(), f.getOld_cer_content(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        row = tableContact.getRow(12);
        setTextFormat(row.getCell(0).getParagraphs().get(0).createRun(), f.getOld_cer_date(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        row = tableContact.getRow(13);
        setTextFormat(row.getCell(0).getParagraphs().get(0).createRun(), f.getNew_cer_content(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        row = tableContact.getRow(14);
        setTextFormat(row.getCell(0).getParagraphs().get(0).createRun(), f.getReason_renew_cer(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

	    //write to word file
	    ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    docx.write(baos);
	    
	    return new ByteArrayInputStream(baos.toByteArray());
	}
}
